<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('创优创标')"/>
    <th:block th:include="include :: layout-latest-css"/>
    <th:block th:include="include :: ztree-css"/>
    <th:block th:include="include :: datetimepicker-css"/>
</head>
<style>
    .search-collapse, .select-table {
        padding-top: 13px;
    }

    .select-list li p {
        margin: 7px 5px 5px 0px;
    }

    .table-striped .table > thead:first-child > tr:first-child > th {
        font-weight: normal;
        font-size: 13px;
        border: 1px solid #cccccc !important;
        border-right: 0px solid #cccccc !important;
    }

    .sumText {
        font-weight: 700;
        color: red;
        font-size: 14px;
    }
</style>
<body class="gray-bg">
<div class="ui-layout-west">
    <div class="box box-main">
        <div class="box-header">
            <div class="box-title">
                <i class="fa icon-grid"></i> 项目名称
            </div>
            <div class="box-tools pull-right">
                <a type="button" class="btn btn-box-tool" href="#" onclick="project()" title="管理项目"><i
                        class="fa fa-edit"></i></a>
                <button type="button" class="btn btn-box-tool" id="btnExpand" title="展开" style="display:none;"><i
                        class="fa fa-chevron-up"></i></button>
                <button type="button" class="btn btn-box-tool" id="btnCollapse" title="折叠"><i
                        class="fa fa-chevron-down"></i></button>
                <button type="button" class="btn btn-box-tool" id="btnRefresh" title="刷新项目"><i
                        class="fa fa-refresh"></i></button>
            </div>
        </div>
        <div class="ui-layout-content">
            <div id="tree" class="ztree"></div>
        </div>
    </div>
</div>

<div class="ui-layout-center">
    <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="region-form">
                    <input type="hidden" id="companyId" name="companyId">
                    <input type="hidden" id="companyName" name="companyName">
                    <input type="hidden" id="proId" name="proId">
                    <input type="hidden" id="programId" name="programId">
                    <input type="hidden" id="programName" name="programName">
                    <div class="select-list">
                        <ul>
                            <li>
                                <p style="width: 90px !important;">项目名称：</p>
                                <input type="text" class="form-control" name="programName" id="progName"
                                       autocomplete='off'/>
                            </li>

                            <li style="margin-left: 50px">
                                <a class="btn btn-primary btn-rounded btn-sm" id="search" onclick="$.table.search()"><i
                                        class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i
                                        class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>
            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-success" id="add"
                   onclick="addView($('#companyId').val(),$('#proId').val(),$('#programName').val())">
                    <i class="fa fa-plus"></i> 新增
                </a>
                <a class="btn btn-primary single" id="edit" onclick="edit()">
                    <i class="fa fa-edit"></i> 修改
                </a>
                <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()">
                    <i class="fa fa-remove"></i> 删除
                </a>
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>

</div>

<th:block th:include="include :: footer"/>
<th:block th:include="include :: layout-latest-js"/>
<th:block th:include="include :: ztree-js"/>
<th:block th:include="include :: datetimepicker-js"/>
<th:block th:include="include :: bootstrap-suggest-js"/>
<th:block th:include="include :: bootstrap-typeahead-js"/>

<script th:inline="javascript">

    /* 模块前缀url */
    var prefix = ctx + "profit";

    /* 初始化方法 */
    $(function () {
        var panehHidden = false;
        if ($(this).width() < 769) {
            panehHidden = true;
        }
        $('body').layout({initClosed: panehHidden, west__size: 300});

        // 隐藏添加按钮
        $('#add').hide();
        $('#edit').hide();
        // 初始化列表
        queryList();

        // 初始化树组件
        queryProjectTree();

        formatDate();

    });

    /* 初始化 列表信息 */
    function queryList() {
        var options = {
            uniqueId: "profitId",
            url: prefix + "/list",
            createUrl: prefix + "/add{id}",
            updateUrl: prefix + "/edit/{id}",
            removeUrl: prefix + "/remove",
            pagination: true,
            toolbar: 'toolbar',
            modalName: "市政收入运营报表",
            columns: [
                {
                    checkbox: true,
                    valign: "middle",
                    align: 'center',
                }, {
                    field: 'companyName',
                    align: 'center',
                    title: '工程公司',
                }, {
                    field: 'programName',
                    align: 'center',
                    title: '项目名称',
                }, {
                    field: 'actualMonth',
                    align: 'center',
                    title: '月份',
                }, {
                    field: 'planProfit',
                    align: 'center',
                    title: '计划利润',
                }, {
                    field: 'actualProfit',
                    align: 'center',
                    title: '实际利润',
                }
            ]
        };
        $.table.init(options);
    }


    /* 初始化 树组件 */
    function queryProjectTree() {
        var fix = ctx + "danManage";
        var url = fix + "/treeData";
        var options = {
            url: url,
            expandLevel: 0,
            onClick: zOnClick
        };
        $.tree.init(options);

        function zOnClick(event, treeId, treeNode) {
            if (treeNode.grade == 1) {
                $("#companyId").val(treeNode.id);
                $("#proId").val("");
                $('#add').hide();
                $('#edit').hide();
                $("#programId").val("");
                $("#programName").val("");
                $.table.search()
            } else if (treeNode.grade == 2) {
                $("#companyId").val(treeNode.pId);
                $("#proId").val(treeNode.id);
                $("#programId").val(treeNode.id);
                $("#programName").val(treeNode.name);
                $('#add').show();
                $('#edit').show();
                $.table.search()
            }
        }
    }

    function addView(companyId, proId, programName) {
        var url = prefix + "/addView?companyId=" + companyId + "&programId=" + proId + "&programName=" + programName;
        $.modal.open("添加" + table.options.modalName, url);
    }

    /* 编辑按钮 */
    function edit() {
        var tableArray = $("#bootstrap-table").bootstrapTable('getSelections');
        console.log(tableArray);
        var url = prefix + '/editView?profitId=' + tableArray[0].profitId;
        $.modal.open("报表编辑", url);
    }

    /* 初始化树组件 > 展开 */
    $('#btnExpand').click(function () {
        $._tree.expandAll(true);
        $(this).hide();
        $('#btnCollapse').show();
    });

    /* 初始化树组件 > 折叠 */
    $('#btnCollapse').click(function () {
        $._tree.expandAll(false);
        $(this).hide();
        $('#btnExpand').show();
    });

    /* 初始化树组件 > 刷新 */
    $('#btnRefresh').click(function () {
        queryProjectTree();
    });

    /*动态详情-详细*/
    function detail(excellenceId) {
        var url = prefix + '/detail/' + excellenceId;
        $.modal.openTab("动态详情", url);
    }

    function refresh() {
        setTimeout(function () {
            $("#bootstrap-table").bootstrapTable('refresh');
        }, 200)

    }

    function formatDate() {
        layui.use('laydate', function () {
            var laydate = layui.laydate;
            var startDate = laydate.render({
                elem: '#startTime',
                type: 'month',
                done: function (value, date) {
                    // 结束时间大于开始时间
                    if (value !== '') {
                        endDate.config.min.year = date.year;
                        endDate.config.min.month = date.month - 1;
                        endDate.config.min.date = date.date;
                    } else {
                        endDate.config.min.year = '';
                        endDate.config.min.month = '';
                        endDate.config.min.date = '';
                    }
                }
            });
            var endDate = laydate.render({
                elem: '#endTime',
                type: 'month',
                done: function (value, date) {
                    // 开始时间小于结束时间
                    if (value !== '') {
                        startDate.config.max.year = date.year;
                        startDate.config.max.month = date.month - 1;
                        startDate.config.max.date = date.date;
                    } else {
                        startDate.config.max.year = '';
                        startDate.config.max.month = '';
                        startDate.config.max.date = '';
                    }
                }
            });
        });
    }

</script>
</body>
</html>